<?php
require_once('Zend/Controller/Plugin/Abstract.php');

class Kbs_Controller_Plugin_Common extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName();

        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');

        if ('admin' == $module) {
            if ($bootstrap->hasPluginResource('AuthHttp')) {
                $bootstrap->bootstrap('AuthHttp');
            }
        }

        $bootstrap->bootstrap('View');
        $view = $bootstrap->getResource('View');
        $moduleParams = $view->$module;

        $view->addBasePath($moduleParams['basePath'])
             ->addHelperPath($moduleParams['helperPath'],
                             $moduleParams['helperPathPrefix']);

        $bootstrap->bootstrap('Layout');
        $layout = $bootstrap->getResource('Layout');
        $layout->setLayoutPath($moduleParams['layoutPath'])
               ->setLayout($moduleParams['layout']);

        if ($bootstrap->hasPluginResource('Dojo')) {
            $bootstrap->bootstrap('Dojo');
        }
    }

    /*
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        //var_dump($request);
    }
    */

    /**
     * Record the current request uri for language switch
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        if ($request->getControllerName() != 'language') {
            $sessionUrl = new Zend_Session_Namespace('currentRail');
            $sessionUrl->currentRail = $_SERVER['REQUEST_URI'];
            //unset($sessionUrl->currentRail);
        }
    }
}